package com.atguigu.chap2;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockingQueueDemo {
    public static void main(String[] args) {
        // 阻塞队列
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);
        new Thread(()->{
            for (int i = 1; i <= 10; i++){
                try {
                    blockingQueue.put(i);
                    System.out.println("生产者生产第" + i + "个馒头，剩余馒头"+ blockingQueue.size());
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "生产者").start();

        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    Thread.sleep(3000);
                    System.out.println("消费者消费第" + blockingQueue.take() + "个馒头，剩余馒头"+ blockingQueue.size());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"消费者").start();
    }
}
